package org.jeecg.modules.rooms.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.rooms.DTO.CesServiceMaintenanceSearchDto;
import org.jeecg.modules.rooms.entity.CesServiceMaintenance;
import org.jeecg.modules.rooms.entity.CesServiceType;
import org.jeecg.modules.rooms.mapper.CesServiceMaintenanceMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务维修 服务实现类
 * </p>
 *
 * @author fendo
 * @since 2023-03-16
 */
@Service
public class CesServiceMaintenanceServiceImpl extends ServiceImpl<CesServiceMaintenanceMapper, CesServiceMaintenance> implements IService<CesServiceMaintenance> {
        @Resource
        private CesServiceMaintenanceMapper cesServiceMaintenanceMapper;

        @Resource
        private CesServiceTypeServiceImpl serviceTypeService;


    /**
     *
     * @param maintenance
     * @return
     */
    public Result create(CesServiceMaintenance maintenance){
        QueryWrapper<CesServiceMaintenance> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq(CesServiceMaintenance.NAME,maintenance.getName());
        queryWrapper.eq(CesServiceMaintenance.HOTEL_ID,maintenance.getHotelId());
        queryWrapper.eq(CesServiceMaintenance.INVALID,false);

        CesServiceMaintenance exit = cesServiceMaintenanceMapper.selectOne(queryWrapper);
        if(!ObjectUtils.isEmpty(exit)) return Result.error("名称重复!");

        maintenance.setCreatAt(LocalDateTime.now());
        maintenance.setUpdateAt(LocalDateTime.now());
        maintenance.setInvalid(false);
        save(maintenance);
        return Result.ok("成功!");
    }

    /**
     * 修改
     * @param maintenance
     * @return
     */
    public Result modify(CesServiceMaintenance maintenance){
        CesServiceMaintenance serviceMaintenance = cesServiceMaintenanceMapper.selectOne(Wrappers.<CesServiceMaintenance>lambdaQuery().eq(CesServiceMaintenance::getId,maintenance.getId()));
       if(ObjectUtils.isEmpty(serviceMaintenance)) return Result.error("数据未找到!");
       if(!serviceMaintenance.getName().equals(maintenance.getName())){
           QueryWrapper<CesServiceMaintenance> queryWrapper =new QueryWrapper<>();
           queryWrapper.eq(CesServiceMaintenance.NAME,maintenance.getName());
           queryWrapper.eq(CesServiceMaintenance.HOTEL_ID,maintenance.getHotelId());
           queryWrapper.eq(CesServiceMaintenance.INVALID,false);
           queryWrapper.ne(CesServiceMaintenance.ID,serviceMaintenance.getId());
           CesServiceMaintenance cesServiceMaintenance = cesServiceMaintenanceMapper.selectOne(queryWrapper);
           if(!ObjectUtils.isEmpty(cesServiceMaintenance)) return Result.error("有重复名称的数据!");
       }

        serviceMaintenance.setUpdateAt(LocalDateTime.now());
        saveOrUpdate(serviceMaintenance);
        return Result.ok("成功!");
    }


    /**
     * 删除
     * @param id
     * @return
     */
    public Result delete(String id,String hotelId){
        CesServiceMaintenance serviceMaintenance = cesServiceMaintenanceMapper.selectOne(Wrappers.<CesServiceMaintenance>lambdaQuery().eq(CesServiceMaintenance::getId,id).eq(CesServiceMaintenance::getHotelId,hotelId).eq(CesServiceMaintenance::getInvalid,false));
        if(ObjectUtils.isEmpty(serviceMaintenance)) return Result.error("数据未找到!");
        serviceMaintenance.setUpdateAt(LocalDateTime.now());
        serviceMaintenance.setInvalid(true);
        saveOrUpdate(serviceMaintenance);
        return Result.ok("删除成功!");
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    public Result deleteBatch(List<String> ids,String hotelId){
        List<CesServiceMaintenance> list = cesServiceMaintenanceMapper.selectList(Wrappers.<CesServiceMaintenance>lambdaQuery().eq(CesServiceMaintenance::getInvalid,false).eq(CesServiceMaintenance::getHotelId,hotelId).in(CesServiceMaintenance::getId,ids));
        if(CollectionUtil.isEmpty(list)) return Result.ok("删除成功!");
        list.forEach(v -> {
            v.setUpdateAt(LocalDateTime.now());
            v.setInvalid(true);
        });

        saveOrUpdateBatch(list);
        return Result.ok("删除成功!");
    }


    public Result fetch(CesServiceMaintenanceSearchDto dto){
        QueryWrapper<CesServiceMaintenance> queryWrapper =new QueryWrapper<>();
        if(StringUtils.isBlank(dto.getTypeId())){
            queryWrapper.eq(CesServiceMaintenance.TYPE,dto.getTypeId());
        }
        queryWrapper.eq(CesServiceMaintenance.HOTEL_ID,dto.getHotelId());

        queryWrapper.eq(CesServiceMaintenance.INVALID,false);
        IPage<CesServiceMaintenance> dataPage = cesServiceMaintenanceMapper.selectPage(new Page<>(dto.getPageNo().intValue(),dto.getPageSize().intValue()),queryWrapper);
        List<CesServiceMaintenance> list = dataPage.getRecords();
        List<String> ids  = list.stream().map(v -> v.getClassify()).collect(Collectors.toList());
        List<CesServiceType> serviceTypes =   serviceTypeService.fetchByIds(ids);
        list.forEach(v -> {

        });
        Map<String,Object> map = new HashMap<>();
        map.put("records", "");
        map.put("total",dataPage.getTotal());
        return Result.OK(map);
    }
}
